package com.hjq.fcloud.widget

import android.content.Context
import android.content.res.TypedArray
import android.text.TextUtils
import android.util.AttributeSet
import android.widget.LinearLayout
import android.widget.RelativeLayout
import cn.bingoogolapple.photopicker.widget.BGASortableNinePhotoLayout
import com.hjq.fcloud.R
import com.hjq.fcloud.ktx.ViewKtx.setVisibility
import com.hjq.widget.layout.SettingBar
import kotlinx.android.synthetic.main.layout_image_upload_view.view.imageUploadTipsTv
import kotlinx.android.synthetic.main.layout_image_upload_view.view.selectedImageView
import kotlinx.android.synthetic.main.layout_image_upload_view.view.settingBar
import kotlinx.android.synthetic.main.layout_video_upload_view.view.countTipsTv

/**
 * 图片上传 view
 */
class ImageUploadView : LinearLayout {
    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        inflateView(attrs)
    }


    private fun inflateView(attrs: AttributeSet?) {
        RelativeLayout.inflate(context, R.layout.layout_image_upload_view, this)

        val array: TypedArray = getContext().obtainStyledAttributes(attrs, R.styleable.ImageUploadView)
        // 文本设置
        if (array.hasValue(R.styleable.ImageUploadView_tips)) {
            setTips(array.getString(R.styleable.ImageUploadView_tips))
        }
        array.recycle()
    }


    fun getSelectedImageView(): BGASortableNinePhotoLayout {
        return selectedImageView
    }

    fun getSettingBar(): SettingBar {
        return settingBar
    }

    /**
     * 是否能编辑，true能
     */
    fun setCanEdit(canEdit:Boolean){
        if(canEdit){
            getSettingBar().setLeftDrawable(R.drawable.ic_star)
        }
        getSelectedImageView().isEditable = canEdit
        imageUploadTipsTv.setVisibility(canEdit)
        countTipsTv.setVisibility(canEdit)
    }

    fun setTips(tips: String?) {
        if(!TextUtils.isEmpty(tips)){
            imageUploadTipsTv.setText(tips)
            imageUploadTipsTv.setVisibility(true)
        }else{
            imageUploadTipsTv.setVisibility(false)
        }
    }
}